package com.yonyong.tao.parts.runspring;

import com.yonyong.tao.controller.ValidController;
import com.yonyong.tao.mapper.dbone.TblDeviceMapper;
import com.yonyong.tao.model.TblDevice;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.ArrayList;
import java.util.List;

/**
 * @Describtion Todo
 * @Author yonyong
 * @Date 2020/4/21 15:27
 * @Version 1.0.0
 **/
@Configuration
public class MyConfig {
    @Bean
    ValidController getValidController(){
        return new ValidController();
    }

    /**
     * 这个表明mybatis的底层运用的是jdk动态代理
     * @return
     */
    @Bean
    TblDeviceMapper getDeviceMapper(){
        Class<?>[] myclass = {TblDeviceMapper.class};
        InvocationHandler invocationHandler = new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                //得到数据库连接代码
                //执行sql代码

                //这里简化，直接返回
                List<TblDevice> list = new ArrayList<>();
                TblDevice tblDevice = new TblDevice();
                tblDevice.setId(66666);
                list.add(tblDevice);
                return list;
            }
        };
        return (TblDeviceMapper)Proxy.newProxyInstance(MyConfig.class.getClassLoader(),myclass,invocationHandler);
    }
}
